/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.eventservice;

import im.general.AbstractListenBehaviour;
import im.general.Config;
//import im.messages.BaseMessage;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gnaskky
 */
public class SendCHListBehaviour extends AbstractListenBehaviour{

    EventServiceAgent es = null;
    /** Creates a new instance of EventServiceListen */

    public SendCHListBehaviour(EventServiceAgent es) throws Exception {
        this.es = es;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.REQUEST );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.GET_CHS_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    @Override
    protected void handleMessage(ACLMessage in_msg) {
        try {
            if( Config.TRACE_LVL > Config.EXTREME_TR_LVL )
                System.out.println("[SendCHListBehaviour]\tGET_CHS_MSG from:\t\t" 
                        + in_msg.getSender().getLocalName() );

            ACLMessage out_msg = new ACLMessage(ACLMessage.CONFIRM);
            out_msg.addReceiver(in_msg.getSender());
            out_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.POST_CHS_MSG);
            out_msg.setContentObject(es.getCHList());
            
            es.send(out_msg);            
            //block
        } catch (IOException ex) {
            Logger.getLogger(SendCHListBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }



 
    }

}
